from PySide6.QtGui import QResizeEvent
from common import WordMergeThread
from ..base import WordFrameBase, ReturnView
from qfluentwidgets import StateToolTip, InfoBar


class WordMergeFrame(ReturnView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.stateToolTip = StateToolTip('Word Merge', '正在合并 Word 文档，请稍后...', self)
        self.stateToolTip.hide()

        self.wordFrame = WordFrameBase()
        self.wordFrame.setTitle('Word 合成')
        self.wordFrame.setLabelText('待合成Word文件路径：', '合成后Word文件路径：')

        self.mergeThread = WordMergeThread(self)

        self.vBox.addWidget(self.wordFrame)

        self.initEvent()

    def initEvent(self):
        # 按钮事件
        self.returnButton.clicked.connect(self.returned_event)
        self.wordFrame.started.connect(self.started_button_event)
        # 线程事件
        self.mergeThread.started.connect(self.started_thread)
        self.mergeThread.error.connect(self.error_thread)
        self.mergeThread.finished.connect(self.finished_thread)

    def started_button_event(self) -> None:
        openFile, saveFile = self.wordFrame.lineEditText()
        if openFile is False:
            return
        self.mergeThread.set_value(openFile, saveFile)
        self.mergeThread.start()

    def started_thread(self):
        self.stateToolTip.show()

    def error_thread(self, msg):
        InfoBar.error('错误', msg, duration=4000, parent=self)

    def finished_thread(self):
        self.stateToolTip.hide()
        InfoBar.success('提示', '文件合成完毕', duration=3000, parent=self)

    def resizeEvent(self, event: QResizeEvent):
        self.stateToolTip.move(event.size().width() - self.stateToolTip.width() - 10, 20)
        super().resizeEvent(event)

    def returned_event(self):
        self.wordFrame.clear()
        self.returned.emit()
